package cn.miwu52.homeworkerp.Controller;

import androidx.appcompat.app.AppCompatActivity;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.hutool.http.HttpDownloader;
import cn.hutool.http.HttpRequest;
import cn.miwu52.homeworkerp.R;
import cn.miwu52.homeworkerp.bean.Product;
import cn.miwu52.homeworkerp.dao.ProductDao;

public class SearchView extends AppCompatActivity {
    private ProductDao productDao;
    private BaseAdapter baseAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        productDao = new ProductDao(this);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search_view);
        Button search_button = findViewById(R.id.search_button);
        EditText search_name = findViewById(R.id.search_name);
        ListView list_item = findViewById(R.id.list_item);

        //搜索按钮
        search_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (search_name.getText().toString().equals("")) {
                    Toast.makeText(getApplicationContext(), "请输入要搜索的商品名", Toast.LENGTH_SHORT).show();
                } else {
                    //获取查询结果
                    List<Product> productByName = productDao.findProductByName(search_name.getText().toString());
                    //处理包装查询结果
                    ArrayList<Map<String, Object>> maps = new ArrayList<>();
                    for (Product product : productByName) {
                        HashMap<String, Object> stringObjectHashMap = new HashMap<>();
                        System.out.println(product);
                        stringObjectHashMap.put("product_name", product.getName());
                        stringObjectHashMap.put("sumInEntrepot", product.getSumInEntrepot());
                        stringObjectHashMap.put("sumOutEntrepot", product.getSumOutEntrepot());
                        stringObjectHashMap.put("InEntrepot", product.getInEntrepot());
                        stringObjectHashMap.put("entrepot", product.getEntrepotId());
                        stringObjectHashMap.put("product_id", product.getId());
                        maps.add(stringObjectHashMap);
                    }
                    String[] from = {"product_name", "sumInEntrepot", "sumOutEntrepot", "InEntrepot", "entrepot", "product_id"};
                    int[] to = {R.id.product_name, R.id.sumInEntrepot, R.id.sumOutEntrepot, R.id.InEntrepot, R.id.entrepot, R.id.product_id};
                    SimpleAdapter simpleAdapter = new SimpleAdapter(SearchView.this, maps, R.layout.prodect_list, from, to);
                    list_item.setAdapter(simpleAdapter);
                }
            }
        });
        //列表点击监听
        list_item.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> adapterView, View view, int i, long l) {
                TextView product_id = view.findViewById(R.id.product_id);
                TextView product_name = view.findViewById(R.id.product_name);
                AlertDialog alertDialog = new AlertDialog.Builder(SearchView.this)
                        .setIcon(R.drawable.erp)
                        .setTitle("询问")
                        .setMessage("请选择你的操作")
                        .setNegativeButton("删除", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                //点击删除按钮
                                AlertDialog alertDialog1 = new AlertDialog.Builder(SearchView.this)
                                        .setIcon(R.drawable.erp)
                                        .setTitle("警告")
                                        .setMessage("确定要删除【" + product_name.getText().toString() + "】吗？")
                                        .setNegativeButton("确定", new DialogInterface.OnClickListener() {
                                            @Override
                                            public void onClick(DialogInterface dialogInterface, int i) {
                                                //执行删除操作
                                                if (productDao.deleteProductById(product_id.getText().toString())) {
                                                    Toast.makeText(getApplicationContext(), "删除成功", Toast.LENGTH_SHORT).show();
                                                    return;
                                                }
                                                Toast.makeText(getApplicationContext(), "删除失败", Toast.LENGTH_SHORT).show();
                                            }
                                        })
                                        .setPositiveButton("取消", new DialogInterface.OnClickListener() {
                                            @Override
                                            public void onClick(DialogInterface dialogInterface, int i) {

                                            }
                                        }).create();
                                alertDialog1.show();

                            }
                        })
                        .setPositiveButton("修改", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                //点击修改按钮
                                View inflate = getLayoutInflater().inflate(R.layout.alertdialog_edit, null);
                                EditText newEntrepotName = inflate.findViewById(R.id.newEntrepotName);
                                AlertDialog alertDialog1 = new AlertDialog.Builder(SearchView.this)
                                        .setIcon(R.drawable.erp)
                                        .setTitle("提示")
                                        .setMessage("请输入新名字")
                                        .setView(inflate)
                                        .setNegativeButton("保存", new DialogInterface.OnClickListener() {
                                            @Override
                                            public void onClick(DialogInterface dialogInterface, int i) {
                                                //修改
                                                if (productDao.changeProductNameById(newEntrepotName.getText().toString(), product_id.getText().toString())) {
                                                    Toast.makeText(getApplicationContext(), "修改成功", Toast.LENGTH_SHORT).show();
                                                    return;
                                                }
                                                Toast.makeText(getApplicationContext(), "修改失败", Toast.LENGTH_SHORT).show();
                                            }
                                        })
                                        .setPositiveButton("取消", new DialogInterface.OnClickListener() {
                                            @Override
                                            public void onClick(DialogInterface dialogInterface, int i) {
                                                //取消
                                            }
                                        }).create();
                                alertDialog1.show();
                            }
                        }).create();
                alertDialog.show();
                return true;
            }
        });

    }
}